/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.workflowDesign.controller;

import com.koron.workflow.common.annotation.CaseUser;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.workflowDesign.bean.ProcessListQueryBean;
import com.koron.workflow.workflowDesign.bean.ProcessUpdateBean;
import com.koron.workflow.workflowDesign.bean.ProcessQueryBean;
import com.koron.workflow.workflowDesign.service.ProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(value = "process", description = "模版")
@RestController
@RequestMapping("/process")
public class ProcessController {
    private static final Logger logger = LoggerFactory.getLogger(ProcessController.class);

    @Autowired
    private ProcessService service;

    @ApiOperation("模版分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "setId", value = "所属模板组ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "模板编号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模板名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态 0:停用，1:启用", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping("queryForList.htm")
    public MessageBean queryForList(@ApiIgnore @CaseUser StaffBean user, @ApiIgnore String appId, @ApiIgnore String setId,
                                    @ApiIgnore String code, @ApiIgnore String name, @ApiIgnore Integer status,
                                    @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        if (page != null) {
            page = page - 1;
            if (page < 0) {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("页码不能小于1");
                return msg;
            }
        }
        try {
            PageBean<ProcessQueryBean> pageBean = ADOConnection.runTask(service, "queryForList", PageBean.class,
                    user, appId, setId, code, name, status, page * pageSize, pageSize);
            msg.setData(pageBean);
        } catch (Exception e) {
            logger.error("分页查询模版失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("根据ID查询模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模版id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/queryForObject.htm")
    public MessageBean queryForObject(@ApiIgnore String id) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        try {
            ProcessQueryBean bean = ADOConnection.runTask(service, "queryForObject", ProcessQueryBean.class, id);
            msg.setData(bean);
        } catch (Exception e) {
            logger.error("根据ID查询模版失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("保存模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "setId", value = "所属模板组ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "模板编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模板名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "工作流描述", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态 0:停用，1:启用", required = false, dataType = "int", paramType = "query"),
    })
    @PostMapping("/save.htm")
    public MessageBean save(@ApiIgnore @CaseUser StaffBean user, @ApiIgnore @RequestBody @Valid ProcessUpdateBean bean) {
        return ADOConnection.runTask(service, "save", MessageBean.class, user, bean);
    }

    @ApiOperation("修改模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模版id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "setId", value = "所属模板组ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "模板编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模板名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "工作流描述", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态 0:停用，1:启用", required = false, dataType = "int", paramType = "query"),
    })
    @PostMapping("/update.htm")
    public MessageBean update(@ApiIgnore @CaseUser StaffBean user, @ApiIgnore @RequestBody @Valid ProcessUpdateBean bean) {
        return ADOConnection.runTask(service, "update", MessageBean.class, user, bean);
    }

    @ApiOperation("根据ID删除模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模版id", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/delete.htm")
    public MessageBean delete(@ApiIgnore String id) {
        return ADOConnection.runTask(service, "delete", MessageBean.class, id);
    }

    @ApiOperation("根据ID复制模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模版id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "模板编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模板名称", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/copyProcess.htm")
    public MessageBean copyProcess(@ApiIgnore String id, @ApiIgnore String code, @ApiIgnore String name) {
        return ADOConnection.runTask(service, "copyProcess", MessageBean.class, id, code, name);
    }

    @ApiOperation("查询用户有设计权限的模版组")
    @GetMapping("/queryDesignProcessList.htm")
    public MessageBean queryDesignProcessList(@ApiIgnore @CaseUser StaffBean user) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        try {
            List<Map> list = ADOConnection.runTask(service, "queryDesignProcessList", List.class, user);
            msg.setData(list);
        } catch (Exception e) {
            logger.error("查询用户有设计权限的模版组失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

}
